<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>显式转换</title>
</head>

<body>
  <script>
    // 转换为Number类型
    console.log('转为Number类型');
    let str = '123'
    console.log(Number(str));  //123
    console.log(Number('pink'));//NaN

    console.log(parseInt('12px'));  //12
    console.log(parseInt('abc12px'));//NaN
    console.log(parseFloat('12.56px'));  //12.56
    console.log(parseFloat('abc12px'));//NaN

    // 转换为Boolean型
    // 只有空字符串,0,undefined,null,NaN,false 会转为false,其它情况都是true
    console.log(Boolean(''));  //false
    console.log(Boolean(0));  //false
    console.log(Boolean(undefined));  //false
    console.log(Boolean(null));  //false
    console.log(Boolean(NaN));  //false
    console.log(Boolean(false));  //false

    console.log(Boolean('NaN'));  //true
    console.log(Boolean('老师'));  //true
    console.log(Boolean(1));  //true
    console.log(Boolean(-2));  //true
    console.log(Boolean(1.1));  //true
    console.log(Boolean(1 + 1));  //true
  </script>
</body>

</html>